package com.wzy.portfolio.component;

import com.alibaba.fastjson.JSON;
import com.wzy.portfolio.mapper.StockMapper;
import com.wzy.portfolio.model.StockMonitorDto;
import com.wzy.portfolio.model.StockPriceDto;
import com.wzy.portfolio.model.WxMsgDto;
import com.wzy.portfolio.utils.DateTool;
import com.wzy.portfolio.utils.RedisUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class StockMonitorCpt {

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Autowired
    private RedisUtil redisUtil;

    @Resource
    private StockMapper stockMapper;

    /**
     * 实时分析
     * @param monitorDto
     * @param priceDto
     */
    public void parseStockMonitor(StockMonitorDto monitorDto, StockPriceDto priceDto) {
        switch (monitorDto.getMonitorType()){
            case "已经购买": yjgm(monitorDto, priceDto); break;
            case "意向标的": yxbd(monitorDto, priceDto); break;
            case "单阳不破": dybp(monitorDto, priceDto); break;
            default:return;
        }
    }

    /**
     * 意向标的实时监控
     * @param monitorDto
     * @param priceDto
     */
    public void yxbd(StockMonitorDto monitorDto, StockPriceDto priceDto){
//        double currentPrice = priceDto.getCurrentPrice();
//        double monitorValue = monitorDto.getMonitorValue();
//        if(currentPrice<monitorValue){//实时价小于监控价,达到监控条件,成交
//            monitorDto.setStatus(0);
//            String tradeMsg = "股票:"+monitorDto.getName()+",监控类型:"+monitorDto.getMonitorType()+",到达监控阈值:"+monitorDto.getMonitorValue()+",自动成交";
//            afterHander(monitorDto,tradeMsg);
//        }
    }

    /**
     * 已经购买监控
     * @param monitorDto
     * @param priceDto
     */
    public void yjgm(StockMonitorDto monitorDto, StockPriceDto priceDto){
        double monitorMinPrice = monitorDto.getMonitorMinPrice();//最低价监控
        double monitorMaxPrice = monitorDto.getMonitorMaxPrice();//最高监控就
        double monitorDayPct = monitorDto.getMonitorDayPct();//日涨幅监控
        double monitorCumulativePct = monitorDto.getMonitorCumulativePct();//累计涨跌幅监控
        double changePct = priceDto.getChangePct();//实时涨幅
        double currentPrice = priceDto.getCurrentPrice();//当前实时价
        String msg =  priceDto.getPriceTime();
        if(currentPrice<=monitorMinPrice){
            msg += "实时价"+currentPrice+"<最低监控价"+monitorMinPrice+",停止";
            monitorDto.setMonitorStatus(0);
            afterHander(monitorDto,msg);
            return;
        }
        if(currentPrice>=monitorMaxPrice){
            msg += "实时价"+currentPrice+">最高监控价"+monitorMinPrice+",停止";
            monitorDto.setMonitorStatus(0);
            afterHander(monitorDto,msg);
            return;
        }
        if(Math.abs(changePct)>=monitorDayPct){
            msg += "实时涨幅"+changePct+"达日波动阈值"+monitorDayPct;
            afterHander(monitorDto,msg);
            return;
        }
        double costPrice = monitorDto.getCostPrice();//成本价
        double cumulativePct = (currentPrice - costPrice) * 100 / costPrice;//累计涨幅
        if(Math.abs(cumulativePct)>=monitorCumulativePct){
            monitorDto.setMonitorStatus(0);
            msg += "累计涨幅"+cumulativePct+"达监控阈值"+monitorCumulativePct+",停止";
            afterHander(monitorDto,msg);
        }
    }

    /**
     * 单阳不破监控
     * @param monitorDto
     * @param priceDto
     */
    public void dybp(StockMonitorDto monitorDto, StockPriceDto priceDto){
//        String name = monitorDto.getName();
//        if(DateTool.isTime1AfterTime2(monitorDto.getMonitorStartDate(),monitorDto.getMonitorEndDate())){
//            monitorDto.setStatus(0);
//            monitorDto.setMonitorResult("股票:"+name+",当前时间:"+DateTool.getNow()+",已达预设监控结束时间:"+monitorDto.getMonitorEndDate()+",停止监控");
//            return;
//        }
//        double currentPrice = priceDto.getCurrentPrice();//实时价格
//        double openValue = monitorDto.getMonitorValue();//监控值(开盘价)
//        double pct = priceDto.getPriceChangePct();
//        double closeValue = monitorDto.getCloseValue() * (1 + 0.03);
//        if(currentPrice>closeValue){//涨8个点，停止监控，不满足下跌
//            monitorDto.setStatus(0);
//            monitorDto.setMonitorResult("股票:"+name+",实时价:"+currentPrice+",已超单阳不破当日收盘价:"+closeValue+"的3个点,停止监控");
//            return;
//        }
//        double minValue = openValue * (1 - 0.03);
//        if(currentPrice<minValue){
//            monitorDto.setStatus(0);
//            monitorDto.setMonitorResult("股票:"+name+"),实时价:"+currentPrice+",已低于单阳不破当日开盘价:"+openValue+"的3个点,破位停止监控");
//            return;
//        }
//        Double monitorVolume = monitorDto.getMonitorVolume();//入监控表当日的成交量
//        Double volume = priceDto.getVolume();//实时成交量
//        if(monitorVolume>0&&volume>monitorVolume*1.2){
//            monitorDto.setStatus(0);
//            monitorDto.setMonitorResult("股票"+name+",实时成交量:"+volume+",已超过单阳不破当日成交量:"+monitorVolume+"的1.2倍,不满足缩量调整,停止监控");
//            return;
//        }
//
//        if(volume<monitorVolume/3){
//            monitorDto.setStatus(0);
//            String tradeMsg = "股票:"+name+",实时成交量:"+volume+",涨跌幅"+pct+",已缩量到单阳不破当日的1/3,自动成交";
//            monitorDto.setMonitorResult(tradeMsg);
//            afterHander(monitorDto,tradeMsg);
//            return;
//        }
//        if(currentPrice<openValue){
//            monitorDto.setStatus(0);
//            String tradeMsg = "股票:"+name+",实时价:"+currentPrice+",涨跌幅:"+pct+"点,已小于单阳不破当日开盘价:"+openValue+",自动成交";
//            monitorDto.setMonitorResult(tradeMsg);
//            afterHander(monitorDto,tradeMsg);
//        }
    }


    /**
     * 满足自动交易后的逻辑
     * @param monitorDto
     */
    public void afterHander(StockMonitorDto monitorDto,String tradeMsg){
        String redisKey = "stock:monitor:"+monitorDto.getStockCode();
        if(!redisUtil.hasKey(redisKey)){
            String title = monitorDto.getStockName()+"-"+monitorDto.getMonitorType();
            String thumbMediaId="2Orxf5OlnFiUgLbjfCbXq2NNJw-rE_PGJm1eEF8A8xnOjdhBb9adIzxwogxvqDhph";
            String content = tradeMsg +"<br><br>数据来源:股票智能交易机器人"+ DateTool.getNow()+"报";
            WxMsgDto wxMsgDto = new WxMsgDto("WangZhengYong",1000006,"mpnews",title, content);
            wxMsgDto.setThumbMediaId(thumbMediaId);
            String pushResult = JSON.toJSONString(wxMsgDto);
            rabbitTemplate.convertAndSend("base.exchange", "weixin.msg.rout", pushResult);
            redisUtil.set(redisKey,pushResult,60*30);
            monitorDto.setNotifyMsg(DateTool.getNow()+"微信通知->"+monitorDto.getStockAccount());
        }
    }

}
